// pages/activity/take_input/take_input.js
var app = getApp();
var request = app.request;
var setting = app.globalData.setting;
import Regions from '../../../utils/regions/Regions.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: setting.url,
    enterAddressPage: false,
    firstEnter: true, //是否第一次进入页面
    showModal:false, 
    goodsList: [],
    select_data:[],//选中的商品组合数据
    mixture_ticket_code:'',
    name: '',
    mobile: '',
    address:[],
    address1: '',
    address2: '',
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //拉取可提商品
    this.getChangePackge(options.mixture_ticket_code);
  
    this.initRegions();
    var take_address = wx.getStorageSync('take_address')
    var name = wx.getStorageSync('take_name')
    var mobile = wx.getStorageSync('take_mobile')
    var address2 = wx.getStorageSync('take_address2')
    this.setData({
      mixture_ticket_code: options.mixture_ticket_code,
      address: take_address ? take_address : [],
      name: name,
      mobile: mobile,
      address2: address2,
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.enterAddressPage) {
      this.data.enterAddressPage = false;
      var addressId = wx.getStorageSync('take:take:address');
      console.log(addressId)

    } else if (this.data.firstEnter ) {
      // this.getDefaultAddress();
    }
    console.log(addressId)
    if (addressId!==undefined){
      this.setData({
        address: addressId
      })
    }
    
    this.data.firstEnter = false;
  },
  /** 初始化区域弹框相关 */
  initRegions: function () {
    var that = this;
    new Regions(this, 'regions', {
      endAreaLevelCall: function (parentId, regionName, address) {
        Object.assign(that.data.address, address);
        that.setData({
          address: that.data.address,
        });
        that.setData({
          'address.province_name': that.data.address.province_name,
          'address.city_name': that.data.address.city_name,
          'address.district_name': that.data.address.district_name,
          'address.twon_name': that.data.address.twon_name,
          'address.province': that.data.address.province,
          'address.city': that.data.address.city,
          'address.district': that.data.address.district,
          'address.twon': that.data.address.twon,
        });
        wx.setStorageSync('take_address', that.data.address)
      }
    });
  },
  getChangePackge_name:function(e){
    this.setData({
      name: e.detail.value
    })
    wx.setStorageSync('take_name', e.detail.value)
  },
  getChangePackge_mobile: function (e) {
    this.setData({
      mobile: e.detail.value
    })
    wx.setStorageSync('take_mobile', e.detail.value)
  },
  
  getChangePackge_address2: function (e) {
    this.setData({
      address2: e.detail.value
    })
    wx.setStorageSync('take_address2', e.detail.value)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  enterAddressPage: function () {
    this.data.enterAddressPage = true;
    wx.navigateTo({
      url: '/pages/user/address_list/address_list?operate=take'
    });
  },
  /** 检查是否已经选了地址 */
  checkAddressList: function () {
    var that = this;
    if (!this.address || this.address == null) {
      wx.showModal({
        title: '请先填写或选择收货地址~',
        success: function (res) {
          if (res.confirm) {
            that.enterAddressPage();
          } else {
            wx.navigateBack();
          }
        },
        fail: function () {
          wx.navigateBack();
        }
      });
      return false;
    }
    return true;
  },

  //选中商品
  setAgree:function(e){
    var that = this;
    var tmp_select_data = [];
    var index = e.currentTarget.dataset.index;
    var goodsList = that.data.goodsList;
    
    for (var i in goodsList) {

      if (i == index) {
        if (goodsList[i].selected) {
            goodsList[i].selected = 0;
          } else {
            goodsList[i].selected = 1;
          }
      }

      //组合选中商品数据
      if (goodsList[i].selected) {
        tmp_select_data.push(i);
      }
      that.setData({
        select_data: tmp_select_data
      });
    }
    
    //更新商品展示
    that.setData({
      goodsList: goodsList,
    })
  },

  closeFn:function(e){
    this.setData({
      showModal:false
    })
  },
  subFn:function(e){
    var that = this;
    
    if (!that.data.address) {
      wx.showModal({
        title: '兑换提示',
        content: '请选择下单地址',
        showCancel: false
      })
      return
    }

    if (that.data.select_data.length <= 0) {
      wx.showModal({
        title: '兑换提示',
        content: '请选择需兑换商品',
        showCancel: false
      })
      return
    }

    // console.log(JSON.stringify(that.data.select_data));
    // var tmp_url = "/api/PickTicket/changeToOrder?mixture_ticket_code=" + that.data.mixture_ticket_code + "&goods_tip_arr=" + JSON.stringify(that.data.select_data) + "&address_id=" + that.data.address.address_id;
    var tmp_url = "/api/PickTicket/changeToOrder?mixture_ticket_code=" + that.data.mixture_ticket_code + "&goods_tip_arr=" + JSON.stringify(that.data.select_data) + "&consignee=" + that.data.name + "&mobile=" + that.data.mobile + "&address=" + that.data.address2 + "&province=" + that.data.address.province + "&city=" + that.data.address.city + "&district=" + that.data.address.district + "&twon=" + that.data.address.twon;
    request.post(tmp_url, {
      isShowLoading: false,
      success: function (res) {
        wx.showModal({
          title: '兑换提示',
          content: '兑换成功',
          showCancel: false
        });
        that.getChangePackge(that.data.mixture_ticket_code);
      }
    });
  }, 
  //拉取可提商品
  getChangePackge: function (mixture_ticket_code) {
    var that = this;
    request.post('/api/PickTicket/ajaxChangePackge?mixture_ticket_code=' + mixture_ticket_code, {
      isShowLoading: false,
      success: function (res) {
        that.setData({ goodsList: res.data.result.goods_list })
        that.setData({ select_num_tip: "(" + res.data.result['goods_num'] + "选" + res.data.result['pick_total'] + ",还剩" + res.data.result['no_pick_total'] + "个可提)" })
      }
    });
  },

  /** 获取用户收货地址 */
  getDefaultAddress: function () {
    var that = this;
    request.get('/api/User/ajaxAddressList', {
      success: function (res) {
        if (res.data.result.length > 0) {
          var address = res.data.result[0] || null;
          that.setData({ address: address });
        } else {
          that.checkAddressList();
        }
      }
    });
  },



})